<?php
/**
 * App客户端中间件
 * panglishan
 * 2020.08.27
 */

namespace App\Http\Middleware;

use App\Repositories\LogRepository;
use Closure;
use Carbon\Carbon;
use App\Exceptions\BasicException;

class AppMiddleware
{

    public function handle($request, Closure $next)
    {

        if (!defined('NOW_TIME')) {
            define('NOW_TIME', Carbon::now()->timestamp);
        }
//        if($request->url() == 'https://app-api.chaofanwl.cn/api/txAuth/auto'){
//            return $next($request);
//        }
        //开启开发者模式,跳过权限控制
        if(!env('BASE_ISDEVELOPER', true)) {

            $sign = $request->header('sign','');
            $token = $request->header('token','');

            if(!$sign) throw new BasicException(10001);

            $timestamp = $request->header('timestamp',0) ? $request->header('timestamp',0) : $request->input('timestamp',0);

            if((time()-$timestamp) > env('APP_TIMEOUT')) throw new BasicException(10002);

            //签名算法--对参数键值对倒序排列,拼接成字符串,加上签名秘钥,md5加密
            $param = $request->all();
            $get_sign = getSign($param);
            if($sign != $get_sign) throw new BasicException(10003);

            if(!empty($token)) {
                //token非空的话,进行用户token校验

            }

        }

        return $next($request);

    }

}
